<?php

/**
 * 留言反馈控制器
 *
 * @since 1.0 2015-1-2 by sutroon
 */
class FeedbackAction extends BaseAction {

    // 留言列表 2015-1-2 by sutroon
    public function feedback_list() {
        if (!$this->RoleInfo['SUPERADMIN']) {
            $where['siteID'] = $this->user_login_data['siteID'];
        }
        $this->itemList('tgeneral_feedback', $where);
    }

    // 系统反馈 2015-1-2 by sutroon
    public function sysfeedback_edit() {
        $this->itemEdit('tgeneral_feedback');
    }

    // 系统反馈数据保存  2015-1-2 by sutroon
    public function sysfeedback_edit_save() {
        $params = create_params();
        $params->addParam('content', 'require', '内容未填写');
        $params->addParam('catalogName', 'require', '类别未选择');
        if ($params->hasError()) {
            $this->act_error($params->getErrors());
        }
        $title = I('title');
        if (!$title) {       
            $title=  sofunc_substring(I('content'), 0,12);
        }
        $params->addData('title', $title);
        $params->addData('userID', $this->user_login_data['id']);
        $params->addData('siteID', $this->user_login_data['siteID']);
        $params->addData('exTags', 'SYS-FEEDBACK');
        $this->itemEditSave('tgeneral_feedback', $params->getParams(), array('title' => $title));
    }

    // 留言回复 2015-1-2 by sutroon
    public function feedback_reply_edit() {
        $id = I('id');
        if (!$id) {
            $this->act_close_dialog(false, '参数有误!');
        }
        $data = M('tgeneral_feedback')->find($id);
        if (!$data) {
            $this->act_close_dialog(false, '记录不存在!');
        }
        if (!$data['keyID']) {
            $data['keyID'] = $data['id'];
        }
        $this->assign('data', $data);
        $this->display();
    }

    // 留言回复数据保存 2015-1-2 by sutroon
    public function feedback_reply_edit_save() {
        $params = create_params();
        $params->addParam('parentID', 'number', '编号无效');
        $params->addParam('content', 'require', '内容未填写');
        if ($params->hasError()) {
            $this->act_error($params->getErrors());
        }
        $keyID = I('keyID');
        $params->addData('keyID', $keyID);
        $title = I('title');
        if (!$title) {
            $title=  sofunc_substring(I('content'), 0,12);
        }
        $params->addData('title', $title);
        $params->addData('userID', $this->user_login_data['id']);
        $params->addData('siteID', $this->user_login_data['siteID']);
        $params->addData('exTags', 'SYS-FEEDBACK');

        $id = I('id');
        $M = M('tgeneral_feedback');
        $result = $M->add($data);
        if (false === $result) {
            $this->act_error('保存失败!'.$M->getDbError());
        } else {
            $M->where("id=$keyID")->setInc('replyCount');
            $this->act_close_dialog(true, '保存成功!');
        }
    }

    // 删除留言 2015-1-2 by sutroon
    public function feedback_delete() {
        $id = I('id');
        if (!$id) {
            $this->act_ajaxReturn(false, '参数无效!');
        }
        $M = M('tgeneral_feedback');
        $result = $M->delete($id);
        if (false === $result) {
            $this->act_ajaxReturn(false, '删除失败!');
        }
        $M->where("keyID=$id")->delete();
        $this->act_ajaxReturn(true, '删除成功!');
    }

    public function feedback_catalog_list() {
        $where = 'exTags=\'NOTICE-CATALOG\' and parentID={0}';
        if (!$this->RoleInfo['SUPERADMIN']) {
            $where .= ' and siteID=' . $this->user_login_data['siteID'];
        }
        $this->itemTreeList('tgeneral_catalog', $where, 0);
    }

}
